package com.sso.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;


import javax.annotation.Resource;

@Configuration
@EnableAuthorizationServer //
public class MyOauthConfig extends AuthorizationServerConfigurerAdapter {
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private AuthenticationManager authenticationManager;
    /**
     * 设置第三方的客户端的信息
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory()
                .withClient("admin") //第三方的名字
                .secret(passwordEncoder.encode("123456")) //密码  加密
                .scopes("all") // 客户端的授权范围
                //.autoApprove(true)// 自动授权
                /**
                 * 支持哪些认证模式
                 * authorization_code 授权码模式
                 * password 密码模式
                 * implicit 简单模式
                 * client_credentials 客户端模式
                 * refresh_token 这个不是一种模式 是支持刷新令牌的意思
                 */
                .authorizedGrantTypes("password","refresh_token") // 授权的类型
                .accessTokenValiditySeconds(60*30)// token生效的时间是1分钟
                .refreshTokenValiditySeconds(60*30*2);
                //.redirectUris("http://www.baidu.com");// 跳转到某一个路径里面  路径的后面会跟一个?code=  code就是我们的授权码
    }

    /**
     * 配置凭证的信息
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
       endpoints.tokenStore(getTokenStore())
               .accessTokenConverter(getConvert())
                .authenticationManager(authenticationManager);
    }

    /**
     * 进行安全的配置  生成token    校验token
     * @param security
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

        security.allowFormAuthenticationForClients()
                .checkTokenAccess("permitAll()") // /oauth/check_token
                .tokenKeyAccess("permitAll"); // 生成token
    }

    /**
     * 配置一个tokenStore
     */
    @Bean
    public TokenStore getTokenStore(){

        return new JwtTokenStore(getConvert());
    }

    /**
     * 构建 令牌
     * @return
     */

    @Bean
    public JwtAccessTokenConverter getConvert(){
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        //
        jwtAccessTokenConverter.setSigningKey("test");
        return  jwtAccessTokenConverter;
    }


}
